import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.HashMap;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author liutao
 * Date 2021/7/16 10:37 上午
 * Description:
 * Version: 1.0
 **/
public class concurentTest {


    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //使用线程join进行等待
        Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("开始执行,睡眠2s");

            }
        });
        thread.start();
        thread.join();
        System.out.println("thread执行完成");


        //futureTask
        FutureTask<String> futureTask = new FutureTask<>(() -> {
//                TimeUnit.MILLISECONDS.sleep(2000L);
            System.out.println("xx");
            return "我是答案";
        });
        futureTask.run();
        String resultFutureTask = futureTask.get();
        System.out.println(resultFutureTask);




        // 使用CountDownLatch 实现线程等待

        CountDownLatch countDownLatch=new CountDownLatch(2);

        ThreadFactory threadFactory= new ThreadFactoryBuilder().setNameFormat("测试").build();

        ThreadPoolExecutor threadPoolExecutor=new ThreadPoolExecutor(2,3,1000,TimeUnit.SECONDS,new LinkedBlockingQueue<Runnable>(),threadFactory);

        HashMap<String,Integer> map=new HashMap<>();
        threadPoolExecutor.execute(()->{
            map.put("start",1);
            countDownLatch.countDown();
        });

        threadPoolExecutor.execute(()->{
            map.put("end",3);
            countDownLatch.countDown();
        });

        countDownLatch.await();
        System.out.println(map.get("start")+map.get("end"));


        //使用CyclicBarrier
        CyclicBarrier cyclicBarrier =new CyclicBarrier(2, () -> {
            System.out.println("=======集合啦=======");
        });

      threadPoolExecutor.execute(()->{
          try {
              System.out.println("DA 到达集合点");
              cyclicBarrier.await();
              System.out.println("DA 开始旅行拉");
          } catch (Exception e) {
              e.printStackTrace();
          }
      });
        threadPoolExecutor.execute(()->{
            try {
                System.out.println("B 到达集合点");
                cyclicBarrier.await();
                System.out.println("B 开始旅行拉");
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        //CyclicBarrier 会自动重置标识位
        threadPoolExecutor.execute(()->{
            try {
                System.out.println("C 到达集合点");
                cyclicBarrier.await();
                System.out.println("C 开始旅行拉");
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        threadPoolExecutor.execute(()->{
            try {
                System.out.println("d 到达集合点");
                cyclicBarrier.await();
                System.out.println("d 开始旅行拉");
            } catch (Exception e) {
                e.printStackTrace();
            }
        });




    }

}
